package org.example.servicelisten.Handler;

import com.auth0.jwt.exceptions.*;
import org.example.servicelisten.exception.InvalidTokenException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.authentication.*;
import org.springframework.security.core.AuthenticationException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.HashMap;
import java.util.Map;

@RestControllerAdvice
public class GlobalExceptionHandler {

    // 处理JWT签名验证异常
    @ExceptionHandler(SignatureVerificationException.class)
    public ResponseEntity<Map<String, Object>> handleSignatureVerificationException(SignatureVerificationException e) {
        return buildErrorResponse(HttpStatus.UNAUTHORIZED, "无效的令牌签名", e.getMessage());
    }

    // 处理JWT令牌过期异常
    @ExceptionHandler(TokenExpiredException.class)
    public ResponseEntity<Map<String, Object>> handleTokenExpiredException(TokenExpiredException e) {
        return buildErrorResponse(HttpStatus.UNAUTHORIZED, "令牌已过期", e.getMessage());
    }

    // 处理JWT令牌无效异常
    @ExceptionHandler(InvalidTokenException.class)
    public ResponseEntity<Map<String, Object>> handleInvalidTokenException(InvalidTokenException e) {
        return buildErrorResponse(HttpStatus.UNAUTHORIZED, "无效的令牌", e.getMessage());
    }

    // 处理JWT算法不支持异常
    @ExceptionHandler(AlgorithmMismatchException.class)
    public ResponseEntity<Map<String, Object>> handleAlgorithmMismatchException(AlgorithmMismatchException e) {
        return buildErrorResponse(HttpStatus.UNAUTHORIZED, "令牌算法不匹配", e.getMessage());
    }

    // 处理JWT声明异常
    @ExceptionHandler(InvalidClaimException.class)
    public ResponseEntity<Map<String, Object>> handleInvalidClaimException(InvalidClaimException e) {
        return buildErrorResponse(HttpStatus.UNAUTHORIZED, "无效的令牌声明", e.getMessage());
    }

    // 处理身份验证异常
    @ExceptionHandler(AuthenticationException.class)
    public ResponseEntity<Map<String, Object>> handleAuthenticationException(AuthenticationException e) {
        return buildErrorResponse(HttpStatus.UNAUTHORIZED, "身份验证失败", e.getMessage());
    }

    // 处理访问被拒绝异常
    @ExceptionHandler(AccessDeniedException.class)
    public ResponseEntity<Map<String, Object>> handleAccessDeniedException(AccessDeniedException e) {
        return buildErrorResponse(HttpStatus.FORBIDDEN, "访问被拒绝", e.getMessage());
    }

    // 处理通用异常
    @ExceptionHandler(Exception.class)
    public ResponseEntity<Map<String, Object>> handleException(Exception e) {
        return buildErrorResponse(HttpStatus.INTERNAL_SERVER_ERROR, "服务器内部错误", e.getMessage());
    }

    // 构建错误响应
    private ResponseEntity<Map<String, Object>> buildErrorResponse(HttpStatus status, String message, String details) {
        Map<String, Object> errorResponse = new HashMap<>();
        errorResponse.put("status", status.value());
        errorResponse.put("message", message);
        errorResponse.put("details", details);
        return new ResponseEntity<>(errorResponse, status);
    }

}